clear
version 8.0
#delimit ;
cd "Z:\interactionmodels\";                       * SET WORKING DIRECTORY;
log using results\fragmentationfig_FIG1.log, replace;


*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      fragmentationfig_FIG1.do                        *;
*       Date:           1/11/07                                         *;
*       Author:         MRG                                             *;
*       Purpose:        Creates fragmentation figure 1 (left)           *;
*       Input File:     STATA_mozaffar.dta, golder1.dta                 *;
*       Output File:    fragmentationfig_FIG1.log                       *;
*       Data Output:    none                                            *;
*       Previous file:                                                  *;
*       Machine:                                                        *;
*     ****************************************************************  *;
*     ****************************************************************  *;

set mem 10m;

use getdata\STATA_mozaffar.dta;

*     ****************************************************************  *;
*     ****************************************************************  *;
*               Fragmentation Figure for Legislative Parties            *;
*               MSG data, FIGURE 1 (left) in paper                      *;
*     ****************************************************************  *;
*     ****************************************************************  *;

regress legparties  fragmentation concentration logmag10 frag_conc logmag10_frag  
logmag10_conc logmag10_frag_conc proximity prescandidate prox_prescandidate, robust;

*     ****************************************************************  *;
*       Create x-axis for modifying variable (LOGMAG10) = JH            *;
*     ****************************************************************  *;

generate JH=((_n-1)/10);    
replace JH=. if _n>30;

generate str1 txt="*";


*     ****************************************************************  *;
*       Grab elements of the matrix required for calculating            *;
*       conditional coefficients and standard errors.                   *;
*     ****************************************************************  *;

matrix b=e(b);
matrix V=e(V);
scalar b1=b[1,1];
scalar b2=b[1,2];
scalar b3=b[1,3];
scalar b4=b[1,4];
scalar b5=b[1,5];
scalar b6=b[1,6];
scalar b7=b[1,7];


scalar varb1=V[1,1];
scalar varb2=V[2,2];
scalar varb3=V[3,3];
scalar varb4=V[4,4];
scalar varb5=V[5,5];
scalar varb6=V[6,6];
scalar varb7=V[7,7];

scalar covb1b4=V[1,4];
scalar covb1b5=V[1,5];
scalar covb1b7=V[1,7];
scalar covb4b5=V[4,5];
scalar covb4b7=V[4,7];
scalar covb5b7=V[5,7];

set more off;

scalar list b1 b2 b3 b4 b5 b6 b7 varb1 varb2 varb3 varb4 varb5 varb6 varb7 
            covb1b4 covb1b5 covb1b7 covb4b5 covb4b7 covb5b7;

*     ****************************************************************  *;
*         Create full range of conditional coefficients for             *;
*       fragmentation                                                   *;
*     ****************************************************************  *;

gen conb0=b1+b4*0+b5*JH+b7*(0*JH) if _n<30;
gen conb1=b1+b4*1+b5*JH+b7*(1*JH) if _n<30;
gen conb2=b1+b4*2+b5*JH+b7*(2*JH) if _n<30;
gen conb3=b1+b4*3+b5*JH+b7*(3*JH) if _n<30;
gen conb4=b1+b4*4+b5*JH+b7*(4*JH) if _n<30;

set more off;


*     ****************************************************************  *;
*           Create full range of conditional standard errors            *;
*     ****************************************************************  *;

gen conse0=sqrt(varb1
                + varb4*(0^2) + varb5*JH^2 + varb7*JH^2*(0^2)
                + 2*0*covb1b4 + 2*JH*covb1b5 + 2*0*JH*covb1b7+2*0*JH*covb4b5
                + 2*(0^2)*JH*covb4b7 + 2*0*(JH^2)*covb5b7)  if _n<30;
                
gen conse1=sqrt(varb1
                + varb4*(1^2) + varb5*JH^2 + varb7*JH^2*(1^2)
                + 2*1*covb1b4 + 2*JH*covb1b5 + 2*1*JH*covb1b7+2*1*JH*covb4b5
                + 2*(1^2)*JH*covb4b7 + 2*1*(JH^2)*covb5b7)  if _n<30;
                
gen conse2=sqrt(varb1
                + varb4*(2^2) + varb5*JH^2 + varb7*JH^2*(2^2)
                + 2*2*covb1b4 + 2*JH*covb1b5 + 2*2*JH*covb1b7+2*2*JH*covb4b5
                + 2*(2^2)*JH*covb4b7 + 2*2*(JH^2)*covb5b7)  if _n<30;
                
gen conse3=sqrt(varb1
                + varb4*(3^2) + varb5*JH^2 + varb7*JH^2*(3^2)
                + 2*3*covb1b4 + 2*JH*covb1b5 + 2*3*JH*covb1b7+2*3*JH*covb4b5
                + 2*(3^2)*JH*covb4b7 + 2*3*(JH^2)*covb5b7)  if _n<30;
                
gen conse4=sqrt(varb1
                + varb4*(4^2) + varb5*JH^2 + varb7*JH^2*(4^2)
                + 2*4*covb1b4 + 2*JH*covb1b5 + 2*4*JH*covb1b7+2*4*JH*covb4b5
                + 2*(4^2)*JH*covb4b7 + 2*4*(JH^2)*covb5b7)  if _n<30;
                
set more off;

*     ****************************************************************  *;
*                           Create t statistics                         *;
*     ****************************************************************  *;

gen t0=conb0/conse0;
gen t1=conb1/conse1;
gen t2=conb2/conse2;
gen t3=conb3/conse3;
gen t4=conb4/conse4;

*     ****************************************************************  *;
*       Generate a variable equal to conditional betas                  *;
*     ****************************************************************  *;

gen consb0=conb0;
gen consb1=conb1;
gen consb2=conb2;
gen consb3=conb3;
gen consb4=conb4;

*     ****************************************************************  *;
*       Replace consb_ = missing if t score not bigger than cutoff      *;
*     ****************************************************************  *;

replace consb0 = . if abs(t0)<2.01;
replace consb1 = . if abs(t1)<2.01;
replace consb2 = . if abs(t2)<2.01;
replace consb3 = . if abs(t3)<2.01;
replace consb4 = . if abs(t4)<2.01;

set textsize 100;

*     ****************************************************************  *;
*       Graph the effect of fragmentation on legislative parties        *;
*       conditional on logmag                                           *;
*     ****************************************************************  *;


graph twoway   line conb0 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb0 JH ,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb1 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb1 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb2 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb2 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb3 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb3 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)        
        ||  ,   
            ysize(6)
            xsize(8)
            xlabel(0 1 2 3, labsize(2.5)) 
            ylabel(-1 0 1 2, labsize(2.5))
            yscale(noline)
            xscale(noline)
            legend(off)
            yline(0, lcolor(black)) yline(-1 1 2 3, lcolor(white)) 
            xtitle("")
            ytitle("")
        xsca(titlegap(2)) ysca(titlegap(4))
        text(2 1 "* indicates significance at the 95% level", justification(left) size(2.5))
        scheme(s2mono) graphregion(fcolor(white))
        graphregion(margin(r=28));
        
set textsize 100;

drop JH conb0 conb1 conb2 conb3 conb4 consb0 consb1 consb2 consb3 consb4 conse0 conse1 conse2 conse3 conse4 t0 t1 t2 t3 t4 txt;

*     ****************************************************************  *;
*                               Save Graph                              *;
*     ****************************************************************  *;

translate @Graph results\FIG1_LEFT_fragmentation_leg.wmf, replace;




*     ****************************************************************  *;
*     ****************************************************************  *;
*     ****************************************************************  *;
*     ****************************************************************  *;
*     ****************************************************************  *;




*     ****************************************************************  *;
*     ****************************************************************  *;
*               Fragmentation Figure for Legislative Parties            *;
*               Golder data, FIGURE 1 (right) in paper                  *;
*     ****************************************************************  *;
*     ****************************************************************  *;

clear;
use getdata\golder1.dta;

*     ****************************************************************  *;
*                   Now do recode of concentration                      *;
*     ****************************************************************  *;

gen conc1 = concentration;
replace conc1 = 3 if (fragmentation==1 & concentration==0);
gen frag_conc1 = fragmentation*conc1;
gen logmag_conc1_nyu = logmag_nyu*conc1;
gen frag_conc1_logmag_nyu = fragmentation*conc1*logmag_nyu;

regress legparties_nyu  fragmentation conc1 logmag_nyu 
        frag_conc_nyu logmag_frag_nyu logmag_conc1_nyu 
        frag_conc1_logmag_nyu proximity_nyu prescandidate_nyu 
        prox_prescandidate_nyu, robust;

*     ****************************************************************  *;
*       Create x-axis for modifying variable (LOGMAG10) = JH            *;
*     ****************************************************************  *;

generate JH=((_n-1)/10);    
replace JH=. if _n>30;

generate str1 txt="*";


*     ****************************************************************  *;
*       Grab elements of the matrix required for calculating            *;
*       conditional coefficients and standard errors.                   *;
*     ****************************************************************  *;

matrix b=e(b);
matrix V=e(V);
scalar b1=b[1,1];
scalar b2=b[1,2];
scalar b3=b[1,3];
scalar b4=b[1,4];
scalar b5=b[1,5];
scalar b6=b[1,6];
scalar b7=b[1,7];



scalar varb1=V[1,1];
scalar varb2=V[2,2];
scalar varb3=V[3,3];
scalar varb4=V[4,4];
scalar varb5=V[5,5];
scalar varb6=V[6,6];
scalar varb7=V[7,7];

scalar covb1b4=V[1,4];
scalar covb1b5=V[1,5];
scalar covb1b7=V[1,7];
scalar covb4b5=V[4,5];
scalar covb4b7=V[4,7];
scalar covb5b7=V[5,7];

set more off;

scalar list b1 b2 b3 b4 b5 b6 b7 varb1 varb2 varb3 varb4 varb5 varb6 varb7 
            covb1b4 covb1b5 covb1b7 covb4b5 covb4b7 covb5b7;

*     ****************************************************************  *;
*         Create full range of conditional coefficients for             *;
*       fragmentation                                                   *;
*     ****************************************************************  *;

gen conb0=b1+b4*0+b5*JH+b7*(0*JH) if _n<30;
gen conb1=b1+b4*1+b5*JH+b7*(1*JH) if _n<30;
gen conb2=b1+b4*2+b5*JH+b7*(2*JH) if _n<30;
gen conb3=b1+b4*3+b5*JH+b7*(3*JH) if _n<30;
gen conb4=b1+b4*4+b5*JH+b7*(4*JH) if _n<30;

set more off;


*     ****************************************************************  *;
*           Create full range of conditional standard errors            *;
*     ****************************************************************  *;

gen conse0=sqrt(varb1
                + varb4*(0^2) + varb5*JH^2 + varb7*JH^2*(0^2)
                + 2*0*covb1b4 + 2*JH*covb1b5 + 2*0*JH*covb1b7+2*0*JH*covb4b5
                + 2*(0^2)*JH*covb4b7 + 2*0*(JH^2)*covb5b7)  if _n<30;
                
gen conse1=sqrt(varb1
                + varb4*(1^2) + varb5*JH^2 + varb7*JH^2*(1^2)
                + 2*1*covb1b4 + 2*JH*covb1b5 + 2*1*JH*covb1b7+2*1*JH*covb4b5
                + 2*(1^2)*JH*covb4b7 + 2*1*(JH^2)*covb5b7)  if _n<30;
                
gen conse2=sqrt(varb1
                + varb4*(2^2) + varb5*JH^2 + varb7*JH^2*(2^2)
                + 2*2*covb1b4 + 2*JH*covb1b5 + 2*2*JH*covb1b7+2*2*JH*covb4b5
                + 2*(2^2)*JH*covb4b7 + 2*2*(JH^2)*covb5b7)  if _n<30;
                
gen conse3=sqrt(varb1
                + varb4*(3^2) + varb5*JH^2 + varb7*JH^2*(3^2)
                + 2*3*covb1b4 + 2*JH*covb1b5 + 2*3*JH*covb1b7+2*3*JH*covb4b5
                + 2*(3^2)*JH*covb4b7 + 2*3*(JH^2)*covb5b7)  if _n<30;
                
gen conse4=sqrt(varb1
                + varb4*(4^2) + varb5*JH^2 + varb7*JH^2*(4^2)
                + 2*4*covb1b4 + 2*JH*covb1b5 + 2*4*JH*covb1b7+2*4*JH*covb4b5
                + 2*(4^2)*JH*covb4b7 + 2*4*(JH^2)*covb5b7)  if _n<30;
                
set more off;

*     ****************************************************************  *;
*                           Create t statistics                         *;
*     ****************************************************************  *;

gen t0=conb0/conse0;
gen t1=conb1/conse1;
gen t2=conb2/conse2;
gen t3=conb3/conse3;
gen t4=conb4/conse4;

*     ****************************************************************  *;
*       Generate a variable equal to conditional betas                  *;
*     ****************************************************************  *;

gen consb0=conb0;
gen consb1=conb1;
gen consb2=conb2;
gen consb3=conb3;
gen consb4=conb4;




*     ****************************************************************  *;
*       Replace consb_ = missing if t score not bigger than cutoff      *;
*     ****************************************************************  *;

replace consb0 = . if abs(t0)<2.01;
replace consb1 = . if abs(t1)<2.01;
replace consb2 = . if abs(t2)<2.01;
replace consb3 = . if abs(t3)<2.01;
replace consb4 = . if abs(t4)<2.01;

set textsize 100;

*     ****************************************************************  *;
*       Graph the effect of fragmentation on legislative parties        *;
*       conditional on logmag                                           *;
*     ****************************************************************  *;

graph twoway   line conb0 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb0 JH ,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb1 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb1 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb2 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb2 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)
        ||  line conb3 JH , clpattern(solid) clwidth(thin)
        ||  scatter consb3 JH,  mlabel(txt) msymbol(i) mlabsize(vsmall) mlabgap(-1.0) mlabposition(11)        
        ||  ,   
            ysize(6)
            xsize(8)
            xlabel(0 1 2 3, labsize(2.5)) 
            ylabel(-0.5 0 0.5 1 , labsize(2.5))
            yscale(noline)
            xscale(noline)
            legend(off)
            yline(0, lcolor(black)) yline(-0.5  0.5 1, lcolor(white)) 
            xtitle("")
            ytitle("")
        xsca(titlegap(2)) ysca(titlegap(4))
        scheme(s2mono) 
        graphregion(fcolor(white))
        graphregion(margin(r=28));
        
       
*     ****************************************************************  *;
*                               Save graph                              *;
*     ****************************************************************  *;

translate @Graph results\FIG1_RIGHT_fragmentation_leg.wmf, replace;

drop JH conb0 conb1 conb2 conb3 conb4 consb0 consb1 consb2 consb3 consb4 conse0 conse1 conse2 conse3 conse4 t0 t1 t2 t3 t4 txt;

*     ****************************************************************  *;
*                           Replication complete                        *;
*     ****************************************************************  *;
                    
log close;
exit;
